<?php
/**
 * 
 * 操作评论
 * 
 * @category System
 * @author Steve Liu
 * @copyright Eshifang 
 */

class Web_Model_Comment
{
	/**
	 * 默认数据库名
	 * 
	 * @var string
	 */
    private static $_table = 'comment';
    
    /**
     * 获得某篇文章的评论
     * 
     * @param int $articleID
     * @return array
     */
    public static function get($articleID)
    {
        $articleID = intval($articleID);
        $data = Eren_Registry::get('DB')->table(self::$_table)
					                  	->where('comment_article_id', '=', $articleID)
					                  	->wAND()
					                  	->where('comment_type', '=', 1)
					                  	->select();
        return $data['data'];
    }
    
    /**
     * 增加文章评论
     * 
     * @param int $articleID
     * @param string $content
     * @throws Eren_Exception
     * @return int
     */
    public static function add($articleID, $content)
    {
        $articleID = intval($articleID);
        $content   = trim($content);
        if(!Web_Model_Member::isSignIn()){
            throw new Eren_Exception('没权限发表评论', -1);
        }else if($content == '' or $content == '<br>'){
            throw new Eren_Exception('评论内容不能为空', -2);
        }else if(!Web_Model_Article::getByID($articleID)){
            throw new Eren_Exception('文章ID不合法', -3);
        }
        $content    = Eren_Filter::addslashes($content);
        $memberID   = Web_Model_Member::getID();
        $memberName = Web_Model_Member::getNickName();
        $now        = Web_Model_Common::getNowTime();
        $fields = array(
        'comment_member_id'   =>$memberID,
        'comment_member_nickname'   => $memberName,
        'comment_article_id'      => $articleID,
        'comment_content'         => $content,
        'comment_type'            =>1,
        'comment_time'            =>$now
        );
        return Eren_Registry::get('DB')->table(self::$_table)->insert($fields);
    }
    
    /**
     * 删除评论
     * 
     * @param int $commentID
     * @throws Eren_Exception
     */
    public static function del($commentID)
    {
        $commentID = intval($commentID);
        if(!Web_Model_Auth::hasRightSetting()){
            throw new Eren_Exception('没权限删除评论', -1);
            return $ret;
        }
        return Eren_Registry::get('DB')->table(self::$_table)
        							   ->where('comment_id', '=', $commentID)
                  					   ->update(array('comment_type'=>2));
    }
    
    /**
     * 获得所有评论
     * 
     * @param int $start
     * @param int $end
     * @return array
     */
    public static function getAll($start, $end)
    {
        $ret = Eren_Registry::get('DB')->table(self::$_table)
					                   ->orderBy('comment_id', 'DESC')
					                   ->limit($start, $end)
					                   ->select();
        $ret['total'] = self::getCount();
        return $ret;
    }
    
    /**
     * 获得评论的数量
     * 
     * @return int
     */
    public static function getCount()
    {
        return Eren_Registry::get('DB')->table(self::$_table)->count();
    }
    
    /**
     * 设置评论的类型,1:正常，2:删除
     * 
     * @param int $commentID
     * @param int $commentType
     * @throws Eren_Exception
     */
    public static function setType($commentID, $commentType)
    {
        $commentID = intval($commentID);
        $commentType = intval($commentType);
        if($commentType<0 || $commentType >2){
            throw new Eren_Exception('级别异常', -1);
        }
        $ret = Eren_Registry::get('DB')->table(self::$_table)
					                   ->where('comment_id', '=', $commentID)
					                   ->update(array('comment_type'=>$commentType));
        return TRUE;
    }
}